package com.xudow.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.xudow.app.R;
import com.xudow.app.ui.adapter.StudentAdapter;
import com.xudow.app.ui.task.StudentListTask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 关联学生Activity
 *
 * @author lukecheng
 */
public class ChildRelateActivity extends BaseActivity {

    private StudentListTask studentListTask;

    private ListView studentsListView;
    private ProgressBar progressBar;

    private StudentAdapter studentAdapter;
    private List<UserProfileWithOther> students;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_relate);

        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        studentsListView = (ListView) findViewById(R.id.students);

        studentListTask = new StudentListTask(this, studentListTaskHandler);
        addTask(studentListTask);
        studentListTask.execute();
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    private Handler studentListTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (StudentListTask.SUCCESS == msg.what) {
                UserProfileWithOther[] studentArr = (UserProfileWithOther[]) msg.getData()
                        .getSerializable("students");

                if (null == studentArr || studentArr.length < 0) {
                    progressBar.setVisibility(View.GONE);

                    return;
                }

                students = new ArrayList<UserProfileWithOther>();
                students.addAll(Arrays.asList(studentArr));

                studentAdapter = new StudentAdapter(ChildRelateActivity.this,
                        R.layout.item_student, students);

                progressBar.setVisibility(View.GONE);
                studentsListView.setVisibility(View.VISIBLE);

                studentsListView.setAdapter(studentAdapter);
            } else {
                String message = getString(R.string.loading_fail);
                Toast.makeText(ChildRelateActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        }
    };

}
